package com.xiaoyu.lingdian.controller;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import com.xiaoyu.lingdian.tool.DateUtil;
import com.xiaoyu.lingdian.tool.RandomUtil;
import com.xiaoyu.lingdian.tool.StringUtil;
import javax.servlet.http.HttpServletResponse;
import com.xiaoyu.lingdian.core.mybatis.page.Page;
import org.springframework.stereotype.Controller;
import com.xiaoyu.lingdian.tool.out.ResultMessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import com.xiaoyu.lingdian.controller.BaseController;
import com.xiaoyu.lingdian.entity.CoreRepairOrder;
import com.xiaoyu.lingdian.service.CoreRepairOrderService;
import com.xiaoyu.lingdian.vo.CoreRepairOrderVO;

@Controller
@RequestMapping(value="/coreRepairOrder")
public class CoreRepairOrderController extends BaseController {

	/**
	* CoreRepairOrder表
	*/
	@Autowired
	private CoreRepairOrderService coreRepairOrderService;
	
	/**
	* 修改-订单状态
	*
	* @param crrorUuid 标识UUID
	* @param crrorUser 下单用户UUID
	* @param crrorStatus 处理状态:1支付中2支付成功3支付失败4已联系5交易成功
	* @param crrorRemarks 备注
	* @return
	*/
	@RequestMapping(value="/update/status", method=RequestMethod.POST)
	public void updateCoreRepairOrderByStatus (String crrorUuid, String crrorUser, Integer crrorStatus, String crrorRemarks, HttpServletResponse response) {
		logger.info("[CoreRepairOrderController]:begin updateCoreRepairOrderByStatus");

		if (StringUtil.isEmpty(crrorUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}
		if (StringUtil.isEmpty(crrorUser)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[下单用户]不能为空!"), response);
			return;
		}
		
		CoreRepairOrder coreRepairOrder = new CoreRepairOrder();
		coreRepairOrder.setCrrorUuid(crrorUuid);
		coreRepairOrder.setCrrorUser(crrorUser);
		coreRepairOrder.setCrrorUdate(new Date());
		coreRepairOrder.setCrrorStatus(crrorStatus);
		coreRepairOrder.setCrrorRemarks(crrorRemarks);

		coreRepairOrderService.updateCoreRepairOrder(coreRepairOrder);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "订单状态修改成功!"),response);
		logger.info("[CoreRepairOrderController]:end updateCoreRepairOrderByStatus");

	}

	/**
	* 添加
	*
	* @param crrorUser 下单用户UUID
	* @param crrorTel 下单用户手机号
	* @param crrorGoods 下单详情
	* @param crrorStatus 处理状态:1支付中2支付成功3支付失败4已联系5交易成功
	* @param crrorTotal 参考价格
	* @param crrorSjFee 支付价格
	* @param crrorSyScore 所用积分
	* @param crrorDkFee 抵扣费用
	* @param crrorRemarks 备注
	* @return
	*/
	@RequestMapping(value="/add/coreRepairOrder", method=RequestMethod.POST)
	public void addCoreRepairOrder (String crrorUser, String crrorTel, String crrorGoods, Integer crrorStatus, String crrorTotal, String crrorSjFee, String crrorSyScore, String crrorDkFee, String crrorRemarks, HttpServletResponse response) {
		logger.info("[CoreRepairOrderController]:begin addCoreRepairOrder");

		if (StringUtil.isEmpty(crrorUser)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[下单用户]不能为空!"), response);
			return;
		}
		if (StringUtil.isEmpty(crrorTel)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[下单用户手机号]不能为空!"), response);
			return;
		}
		if (StringUtil.isEmpty(crrorGoods)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[下单详情]不能为空!"), response);
			return;
		}
		if (StringUtil.isEmpty(crrorTotal)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[参考价格]不能为空!"), response);
			return;
		}
				CoreRepairOrder coreRepairOrder = new CoreRepairOrder();
		String uuid = RandomUtil.generateString(16);
		coreRepairOrder.setCrrorUuid(uuid);
		coreRepairOrder.setCrrorUser(crrorUser);
		coreRepairOrder.setCrrorTel(crrorTel);
		coreRepairOrder.setCrrorGoods(crrorGoods);
		coreRepairOrder.setCrrorCdate(new Date());
		coreRepairOrder.setCrrorUdate(new Date());
		coreRepairOrder.setCrrorStatus(crrorStatus);
		coreRepairOrder.setCrrorTotal(crrorTotal);
		coreRepairOrder.setCrrorSjFee(crrorSjFee);
		coreRepairOrder.setCrrorSyScore(crrorSyScore);
		coreRepairOrder.setCrrorDkFee(crrorDkFee);
		coreRepairOrder.setCrrorRemarks(crrorRemarks);

		coreRepairOrderService.insertCoreRepairOrder(coreRepairOrder);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "新增成功!"),response);
		logger.info("[CoreRepairOrderController]:end addCoreRepairOrder");

	}

	/**
	* 修改
	*
	* @param crrorUuid 标识UUID
	* @param crrorUser 下单用户UUID
	* @param crrorTel 下单用户手机号
	* @param crrorGoods 下单详情
	* @param crrorStatus 处理状态:1支付中2支付成功3支付失败4已联系5交易成功
	* @param crrorTotal 参考价格
	* @param crrorSjFee 支付价格
	* @param crrorSyScore 所用积分
	* @param crrorDkFee 抵扣费用
	* @param crrorRemarks 备注
	* @return
	*/
	@RequestMapping(value="/update/coreRepairOrder", method=RequestMethod.POST)
	public void updateCoreRepairOrder (String crrorUuid, String crrorUser, String crrorTel, String crrorGoods, Integer crrorStatus, String crrorTotal, String crrorSjFee, String crrorSyScore, String crrorDkFee, String crrorRemarks, HttpServletResponse response) {
		logger.info("[CoreRepairOrderController]:begin updateCoreRepairOrder");
		if (StringUtil.isEmpty(crrorUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}
		if (StringUtil.isEmpty(crrorUser)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[下单用户]不能为空!"), response);
			return;
		}
		if (StringUtil.isEmpty(crrorTel)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[下单用户手机号]不能为空!"), response);
			return;
		}
		if (StringUtil.isEmpty(crrorGoods)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[下单详情]不能为空!"), response);
			return;
		}
		if (StringUtil.isEmpty(crrorTotal)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[参考价格]不能为空!"), response);
			return;
		}
				CoreRepairOrder coreRepairOrder = new CoreRepairOrder();
		coreRepairOrder.setCrrorUuid(crrorUuid);
		coreRepairOrder.setCrrorUser(crrorUser);
		coreRepairOrder.setCrrorTel(crrorTel);
		coreRepairOrder.setCrrorGoods(crrorGoods);
		coreRepairOrder.setCrrorUdate(new Date());
		coreRepairOrder.setCrrorStatus(crrorStatus);
		coreRepairOrder.setCrrorTotal(crrorTotal);
		coreRepairOrder.setCrrorSjFee(crrorSjFee);
		coreRepairOrder.setCrrorSyScore(crrorSyScore);
		coreRepairOrder.setCrrorDkFee(crrorDkFee);
		coreRepairOrder.setCrrorRemarks(crrorRemarks);

		coreRepairOrderService.updateCoreRepairOrder(coreRepairOrder);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "修改成功!"),response);
		logger.info("[CoreRepairOrderController]:end updateCoreRepairOrder");

	}

	/**
	* 删除
	*
	* @param crrorUuid 标识UUID
	* @return
	*/
	@RequestMapping(value="/delete/one", method=RequestMethod.POST)
	public void deleteCoreRepairOrder (String crrorUuid, HttpServletResponse response) {
		logger.info("[CoreRepairOrderController]:begin deleteCoreRepairOrder");
		if (StringUtil.isEmpty(crrorUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}
		CoreRepairOrder coreRepairOrder = new CoreRepairOrder();
		coreRepairOrder.setCrrorUuid(crrorUuid);

		coreRepairOrderService.deleteCoreRepairOrder(coreRepairOrder);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "删除成功!"),response);
		logger.info("[CoreRepairOrderController]:end deleteCoreRepairOrder");

	}

	/**
	* 批量删除
	*
	* @param crrorUuids UUID集合

	* @return
	*/
	@RequestMapping(value="/delete/batch", method=RequestMethod.POST)
	public void deleteBatchCoreRepairOrder (String crrorUuids, HttpServletResponse response) {
		logger.info("[CoreRepairOrderController]:begin deleteBatchCoreRepairOrder");
		if (StringUtil.isEmpty(crrorUuids)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[UUID集合]不能为空!"), response);
			return;
		}
		String[] uuids=crrorUuids.split("\\|");
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < uuids.length; i++) {
			list.add(uuids[i]);
		}
		if (list.size() == 0) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请选择批量删除对象！"), response);
			return;
		}
		coreRepairOrderService.deleteBatchByIds(list);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "批量删除成功!"),response);
		logger.info("[CoreRepairOrderController]:end deleteBatchCoreRepairOrder");

	}

	/**
	* 获取单个
	*
	* @param crrorUuid 标识UUID
	* @return
	*/
	@RequestMapping(value="/views", method=RequestMethod.POST)
	public void viewsCoreRepairOrder (String crrorUuid, HttpServletResponse response) {
		logger.info("[CoreRepairOrderController]:begin viewsCoreRepairOrder");
		if (StringUtil.isEmpty(crrorUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}
		CoreRepairOrder coreRepairOrder = new CoreRepairOrder();
		coreRepairOrder.setCrrorUuid(crrorUuid);

		coreRepairOrder = coreRepairOrderService.getCoreRepairOrder(coreRepairOrder);

		CoreRepairOrderVO coreRepairOrderVO = new CoreRepairOrderVO();
		coreRepairOrderVO.convertPOToVO(coreRepairOrder);
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "获取单个信息成功", coreRepairOrderVO), response);
		logger.info("[CoreRepairOrderController]:end viewsCoreRepairOrder");

	}

	/**
	* 获取列表<List>	
	* @return
	*/
	@RequestMapping(value="/find/all", method=RequestMethod.POST)
	public void findCoreRepairOrderList (HttpServletResponse response) {
		logger.info("[CoreRepairOrderController]:begin findCoreRepairOrderList");
		List<CoreRepairOrder> lists = coreRepairOrderService.findCoreRepairOrderList();
		List<CoreRepairOrderVO> vos = new ArrayList<CoreRepairOrderVO>();
		CoreRepairOrderVO vo;
		for (CoreRepairOrder coreRepairOrder : lists) {
			vo = new CoreRepairOrderVO();
			vo.setCrrorUuid(coreRepairOrder.getCrrorUuid());
			vo.setCrrorUser(coreRepairOrder.getCrrorUser());
			vo.setCrusrName(coreRepairOrder.getCrusrName());
			vo.setCrrorTel(coreRepairOrder.getCrrorTel());
			vo.setCrrorGoods(coreRepairOrder.getCrrorGoods());
			vo.setCrrorCdate(DateUtil.formatTimesTampDate(coreRepairOrder.getCrrorCdate()));
			vo.setCrrorUdate(DateUtil.formatTimesTampDate(coreRepairOrder.getCrrorUdate()));
			vo.setCrrorStatus(coreRepairOrder.getCrrorStatus());
			vo.setCrrorTotal(coreRepairOrder.getCrrorTotal());
			vo.setCrrorSjFee(coreRepairOrder.getCrrorSjFee());
			vo.setCrrorSyScore(coreRepairOrder.getCrrorSyScore());
			vo.setCrrorDkFee(coreRepairOrder.getCrrorDkFee());
			vo.setCrrorRemarks(coreRepairOrder.getCrrorRemarks());
			vos.add(vo);
		}
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "list列表获取成功!", vos),response);
		logger.info("[CoreRepairOrderController]:end findCoreRepairOrderList");

	}

	/**
	* 获取列表<Page>	
	* @param pageNum 页码
	* @param pageSize 页数
	* @param crrorUser 下单用户
	* @param crusrName 下单用户账户
	* @param crrorStatus 处理状态
	* @return
	*/
	@RequestMapping(value="/find/by/cnd", method=RequestMethod.POST)
	public void findCoreRepairOrderPage (Integer pageNum, Integer pageSize, String crrorUser, String crusrName, Integer crrorStatus, HttpServletResponse response) {
		logger.info("[CoreRepairOrderController]:begin findCoreRepairOrderPage");
		if (pageNum == null || pageNum == 0) {
			pageNum = 1;
		}
		if (pageSize == null || pageSize == 0) {
			pageSize = 10;
		}
		Page<CoreRepairOrder> page = coreRepairOrderService.findCoreRepairOrderPage(pageNum, pageSize, crrorUser, crusrName, crrorStatus);
		Page<CoreRepairOrderVO> pageVO = new Page<CoreRepairOrderVO>(page.getPageNumber(), page.getPageSize(), page.getTotalCount());
		List<CoreRepairOrderVO> vos = new ArrayList<CoreRepairOrderVO>();
		CoreRepairOrderVO vo;
		for (CoreRepairOrder coreRepairOrder : page.getResult()) {
			vo = new CoreRepairOrderVO();
			vo.setCrrorUuid(coreRepairOrder.getCrrorUuid());
			vo.setCrrorUser(coreRepairOrder.getCrrorUser());
			vo.setCrusrName(coreRepairOrder.getCrusrName());
			vo.setCrrorTel(coreRepairOrder.getCrrorTel());
			vo.setCrrorGoods(coreRepairOrder.getCrrorGoods());
			vo.setCrrorCdate(DateUtil.formatTimesTampDate(coreRepairOrder.getCrrorCdate()));
			vo.setCrrorUdate(DateUtil.formatTimesTampDate(coreRepairOrder.getCrrorUdate()));
			vo.setCrrorStatus(coreRepairOrder.getCrrorStatus());
			vo.setCrrorTotal(coreRepairOrder.getCrrorTotal());
			vo.setCrrorSjFee(coreRepairOrder.getCrrorSjFee());
			vo.setCrrorSyScore(coreRepairOrder.getCrrorSyScore());
			vo.setCrrorDkFee(coreRepairOrder.getCrrorDkFee());
			vo.setCrrorRemarks(coreRepairOrder.getCrrorRemarks());
			vos.add(vo);
		}
		pageVO.setResult(vos);
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "page列表获取成功!", pageVO),response);
		logger.info("[CoreRepairOrderController]:end findCoreRepairOrderPage");

	}
//<=================定制内容开始==============
//==================定制内容结束==============>

}